package cn.com.fig.control;

import cn.com.fig.common.ReplyMsg;
import cn.com.fig.portal.ILoginService;
import cn.com.fig.portal.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 登录和退出相关操作
 */
@Controller
@RequestMapping("/LoginControl")
public class LoginControl {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    ILoginService loginService;

    /**
     * 登陆验证用户名密码是否正确，验证结果返回调用方。
     * 验证成功后初始化用户session信息
     * @param userId
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public @ResponseBody
    ReplyMsg login(HttpServletRequest req, HttpServletResponse res, String userId, String password){
        ReplyMsg msg = new ReplyMsg();
        Map map = loginService.loginCheck(userId,password);
        String retCode = (String)map.get("retCode");
        if(retCode.equals("-1")){
            msg.setRetCode(retCode);
            msg.setRetMsg("用户不存在");
            return msg;
        }
        if(retCode.equals("-2")){
            msg.setRetCode(retCode);
            msg.setRetMsg("用户名或密码错误");
            return msg;
        }
//        初始化用户session信息
        map = loginService.loadSession(req,res,userId);
        if(map == null){
            msg.setRetCode("-3");
            msg.setRetMsg("用户未分配岗责和身份信息");
            return msg;
        }
        User user = (User)map.get("user");
        req.setAttribute("user", user);
        req.setAttribute("menuList", map.get("menuList"));
        req.getSession().setAttribute("user", user);
        req.getSession().setAttribute("userId",userId);
        return msg;
    }

    /**
     * 提供给子系统调用，验证token是否有效
     * @param req
     * @param res
     * @param userId
     * @param token
     * @param sessionId
     * @return
     */
    @RequestMapping("/clientCheck")
    public @ResponseBody Map clientCheck(HttpServletRequest req, HttpServletResponse res, String userId,String token,String sessionId){
        return loginService.tokenCheck(req,res,userId,token,sessionId);
    }
    /**
     * 用户登陆成功后，调用该方法进行首页跳转
     * @param req
     * @param res
     * @param userId
     * @throws IOException
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest req, HttpServletResponse res, String userId) throws IOException {
        String sessionId = loginService.getSessionid(req);
        String token = loginService.getToken(req);
        Map map = loginService.tokenCheck(req,res,userId,token,sessionId);
        User user = (User)map.get("user");
        String mainPage = user.getFirstIdentity().getMainPage();
        if(mainPage != null || mainPage.length() > 10){
            return mainPage;
        }else {
            return"/index.html";
        }
    }

    /**
     * 切换用户身份，同时会重新加载用户权限信息
     * @param req
     * @param res
     * @param userId
     * @throws IOException
     */
    @RequestMapping("/identityChange")
    public String identityChange(HttpServletRequest req, HttpServletResponse res, String userId,String identityId) throws IOException {
        Map map = loginService.identityChange(req,res,userId,identityId);
        User user = (User)map.get("user");
        String mainPage = user.getCurrentIdentity().getMainPage();
        req.setAttribute("user", user);
        req.setAttribute("menuList", map.get("menuList"));
        if(mainPage != null || mainPage.length() > 10){
            return mainPage;
        }else{
            return"/index.html";
        }

    }
    @RequestMapping("/logout")
    public void logout(HttpServletRequest req, HttpServletResponse res,String userId) throws IOException {
        loginService.clearSession(req);
        req.getSession().invalidate();
        res.sendRedirect("/login.html");
    }
}
